package org.rsbot.script.methods;

import java.util.LinkedHashSet;
import java.util.Set;
import java.util.Set<Lorg.rsbot.script.wrappers.RSObject;>;
import org.rsbot.client.Client;
import org.rsbot.client.RSAnimableNode;
import org.rsbot.client.RSGround;
import org.rsbot.script.util.Filter;
import org.rsbot.script.wrappers.RSObject.Type;
import org.rsbot.script.wrappers.RSObjectDef;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.script.wrappers.RSTile;

public class Objects extends MethodProvider
{
  public static final int TYPE_INTERACTABLE = 1;
  public static final int TYPE_FLOOR_DECORATION = 2;
  public static final int TYPE_BOUNDARY = 4;
  public static final int TYPE_WALL_DECORATION = 8;
  public static final Filter<org.rsbot.script.wrappers.RSObject> ALL_FILTER = new Filter() {
    public boolean accept(org.rsbot.script.wrappers.RSObject paramRSObject) {
      return true;
    }
  };

  Objects(MethodContext paramMethodContext)
  {
    super(paramMethodContext);
  }

  public org.rsbot.script.wrappers.RSObject[] getAll()
  {
    return getAll(ALL_FILTER);
  }

  public org.rsbot.script.wrappers.RSObject[] getAll(Filter<org.rsbot.script.wrappers.RSObject> paramFilter)
  {
    LinkedHashSet localLinkedHashSet = new LinkedHashSet();
    for (int i = 0; i < 104; i++) {
      for (int j = 0; j < 104; j++) {
        for (org.rsbot.script.wrappers.RSObject localRSObject : getAtLocal(i, j, -1)) {
          if (paramFilter.accept(localRSObject)) {
            localLinkedHashSet.add(localRSObject);
          }
        }
      }
    }
    return (org.rsbot.script.wrappers.RSObject[])localLinkedHashSet.toArray(new org.rsbot.script.wrappers.RSObject[localLinkedHashSet.size()]);
  }

  public org.rsbot.script.wrappers.RSObject getNearest(Filter<org.rsbot.script.wrappers.RSObject> paramFilter)
  {
    Object localObject = null;
    double d1 = -1.0D;
    for (int i = 0; i < 104; i++) {
      for (int j = 0; j < 104; j++) {
        Set localSet = getAtLocal(i, j, -1);
        for (org.rsbot.script.wrappers.RSObject localRSObject : localSet) {
          if (paramFilter.accept(localRSObject)) {
            double d2 = this.methods.calc.distanceBetween(this.methods.players.getMyPlayer().getLocation(), localRSObject.getLocation());
            if (localObject == null) {
              d1 = d2;
              localObject = localRSObject; break;
            }if (d2 >= d1) break;
            localObject = localRSObject;
            d1 = d2; break;
          }
        }
      }

    }

    return localObject;
  }

  public org.rsbot.script.wrappers.RSObject getNearest(int[] paramArrayOfInt)
  {
    return getNearest(new Filter(paramArrayOfInt) {
      public boolean accept(org.rsbot.script.wrappers.RSObject paramRSObject) {
        for (int k : this.val$ids) {
          if (paramRSObject.getID() == k) {
            return true;
          }
        }
        return false;
      }
    });
  }

  public org.rsbot.script.wrappers.RSObject getNearest(String[] paramArrayOfString)
  {
    return getNearest(new Filter(paramArrayOfString) {
      public boolean accept(org.rsbot.script.wrappers.RSObject paramRSObject) {
        RSObjectDef localRSObjectDef = paramRSObject.getDef();
        if (localRSObjectDef != null) {
          for (String str : this.val$names) {
            if (str.equals(localRSObjectDef.getName())) {
              return true;
            }
          }
        }
        return false;
      }
    });
  }

  public org.rsbot.script.wrappers.RSObject getTopAt(RSTile paramRSTile)
  {
    return getTopAt(paramRSTile, -1);
  }

  public org.rsbot.script.wrappers.RSObject getTopAt(RSTile paramRSTile, int paramInt)
  {
    org.rsbot.script.wrappers.RSObject[] arrayOfRSObject = getAt(paramRSTile, paramInt);
    return arrayOfRSObject.length > 0 ? arrayOfRSObject[0] : null;
  }

  public org.rsbot.script.wrappers.RSObject[] getAt(RSTile paramRSTile, int paramInt)
  {
    Set localSet = getAtLocal(paramRSTile.getX() - this.methods.client.getBaseX(), paramRSTile.getY() - this.methods.client.getBaseY(), paramInt);
    return (org.rsbot.script.wrappers.RSObject[])localSet.toArray(new org.rsbot.script.wrappers.RSObject[localSet.size()]);
  }

  public org.rsbot.script.wrappers.RSObject[] getAllAt(RSTile paramRSTile)
  {
    Set localSet = getAtLocal(paramRSTile.getX() - this.methods.client.getBaseX(), paramRSTile.getY() - this.methods.client.getBaseY(), -1);
    return (org.rsbot.script.wrappers.RSObject[])localSet.toArray(new org.rsbot.script.wrappers.RSObject[localSet.size()]);
  }

  private Set<org.rsbot.script.wrappers.RSObject> getAtLocal(int paramInt1, int paramInt2, int paramInt3) {
    Client localClient = this.methods.client;
    LinkedHashSet localLinkedHashSet = new LinkedHashSet();
    if (localClient.getRSGroundArray() == null) {
      return localLinkedHashSet;
    }
    try
    {
      int i = localClient.getPlane();
      RSGround localRSGround = localClient.getRSGroundArray()[i][paramInt1][paramInt2];

      if (localRSGround != null)
      {
        paramInt1 += this.methods.client.getBaseX();
        paramInt2 += this.methods.client.getBaseY();
        Object localObject;
        org.rsbot.client.RSObject localRSObject;
        if ((paramInt3 & 0x1) != 0) {
          for (RSAnimableNode localRSAnimableNode = localRSGround.getRSAnimableList(); localRSAnimableNode != null; localRSAnimableNode = localRSAnimableNode.getNext()) {
            localObject = localRSAnimableNode.getRSAnimable();
            if ((localObject != null) && ((localObject instanceof org.rsbot.client.RSObject))) {
              localRSObject = (org.rsbot.client.RSObject)localObject;
              if (localRSObject.getID() != -1) {
                localLinkedHashSet.add(new org.rsbot.script.wrappers.RSObject(this.methods, localRSObject, RSObject.Type.INTERACTABLE, i));
              }
            }
          }

        }

        if ((paramInt3 & 0x2) != 0) {
          localObject = localRSGround.getFloorDecoration();
          if (localObject != null) {
            localRSObject = (org.rsbot.client.RSObject)localObject;
            if (localRSObject.getID() != -1) {
              localLinkedHashSet.add(new org.rsbot.script.wrappers.RSObject(this.methods, localRSObject, RSObject.Type.FLOOR_DECORATION, i));
            }

          }

        }

        if ((paramInt3 & 0x4) != 0) {
          localObject = localRSGround.getBoundary1();
          if (localObject != null) {
            localRSObject = (org.rsbot.client.RSObject)localObject;
            if (localRSObject.getID() != -1) {
              localLinkedHashSet.add(new org.rsbot.script.wrappers.RSObject(this.methods, localRSObject, RSObject.Type.BOUNDARY, i));
            }
          }
          localObject = localRSGround.getBoundary2();
          if (localObject != null) {
            localRSObject = (org.rsbot.client.RSObject)localObject;
            if (localRSObject.getID() != -1) {
              localLinkedHashSet.add(new org.rsbot.script.wrappers.RSObject(this.methods, localRSObject, RSObject.Type.BOUNDARY, i));
            }
          }
        }

        if ((paramInt3 & 0x8) != 0) {
          localObject = localRSGround.getWallDecoration1();
          if (localObject != null) {
            localRSObject = (org.rsbot.client.RSObject)localObject;
            if (localRSObject.getID() != -1) {
              localLinkedHashSet.add(new org.rsbot.script.wrappers.RSObject(this.methods, localRSObject, RSObject.Type.WALL_DECORATION, i));
            }
          }
          localObject = localRSGround.getWallDecoration2();
          if (localObject != null) {
            localRSObject = (org.rsbot.client.RSObject)localObject;
            if (localRSObject.getID() != -1)
              localLinkedHashSet.add(new org.rsbot.script.wrappers.RSObject(this.methods, localRSObject, RSObject.Type.WALL_DECORATION, i));
          }
        }
      }
    } catch (Exception localException) {
    }
    return (Set<org.rsbot.script.wrappers.RSObject>)localLinkedHashSet;
  }
}